package com.eight.travel.service.impl;


import com.eight.travel.dao.HotelDao;
import com.eight.travel.pojo.Hotel;
import com.eight.travel.pojo.User;
import com.eight.travel.service.HotelService;
import com.eight.travel.utils.BaseResp;
import com.eight.travel.vo.HotelCheckin;
import com.eight.travel.vo.HotelCom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service

public class HotelServiceImpl implements HotelService {

    @Autowired
    HotelDao hotelDao;
    @Autowired
    OrderRoomImpl orderRoomImpl;
    public BaseResp baseResp= new BaseResp();
    //搜索的模糊查询
    @Override
    public BaseResp findbylike(String name) {
        List<Hotel> all = hotelDao.findbylike(name);
        baseResp.setData(all);
        return baseResp;
    }
//全查
    @Override
    public BaseResp findAll() {
        List<Hotel> hotels = hotelDao.finAll();
        baseResp.setData(hotels);
        return baseResp;
    }

    @Override
    public BaseResp finhotelhot(String address) {
        List<Hotel> finhotelhot = hotelDao.finhotelhot(address);
        baseResp.setData(finhotelhot);
        return baseResp;
    }

    @Override
    public BaseResp finhotelstart(String address, Integer start) {
        List<Hotel> finhotelstart = hotelDao.finhotelstart(address, start);
        baseResp.setData(finhotelstart);
        return baseResp;
    }

    @Override
    public BaseResp findhotelprice(String address) {
        List<Hotel> findhotelprice = hotelDao.findhotelprice(address);
        baseResp.setData(findhotelprice);
        return baseResp;
    }

    @Override
    public BaseResp outroom(Integer uid, String number) {
        HotelCheckin findbyuid = hotelDao.findbyuid(uid, number);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String systime = df.format(System.currentTimeMillis());
        System.out.println("systime ======== " + systime);
        String startime = df.format(findbyuid.getStartime());
        //System.out.println("startime = " + startime);
        try {
            Date systimes = df.parse(systime);
            Date satrt = df.parse(startime);
//            System.out.println("satrt = " + satrt);
//            System.out.println("systimes ======== " + systimes);
            long h=3110400l;
            long l1 = systimes.getTime() - satrt.getTime();
            //System.out.println("l1 =========== " + l1);

            long l = (systimes.getTime() - satrt.getTime())/h;
            System.out.println("l !!!!!!!!!= " + l);
            if (l < 2) {
                baseResp.setMsg("退房成功!!!");
            }else {
                baseResp.setMsg("退房失败!!!已经超出规定时间请联系客服");
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return baseResp;
    }
    //优惠
    @Override
    public BaseResp youhui(Integer uid,String address) {
        HotelCheckin youhui = hotelDao.youhui(uid);
        Integer count=null;
        if(youhui != null){
            count = youhui.getCount();
        }else {
            count = 0;
        }

        Double price=null;
        List<Hotel> findhotelprice = hotelDao.findhotelprice(address);
        if (count>2){
            for (Hotel hotel : findhotelprice) {
                String hotelprice = hotel.getHotelprice();
                price = Double.valueOf(hotelprice)*0.8;
                baseResp.setData(price);
            }
        }else if (count!= null) {
            for (Hotel hotel : findhotelprice) {
                String hotelprice = hotel.getHotelprice();
                price = Double.valueOf(hotelprice);
                baseResp.setData(price);
            }
        }
        return baseResp;
    }
//查看评论,只显示良好评论
    @Override
    public BaseResp ViewCom(Integer hid) {
        List<HotelCom> hotelComs = hotelDao.ViewCom(hid);
        List<HotelCom> goodCom = new ArrayList<>();
        for(int i = 0; i < hotelComs.size(); i++){
            String hotelcom = hotelComs.get(i).getHotelcom();
            if (!(hotelcom.contains("这个酒店不好"))
                    &&!(hotelcom.contains("体验不好"))
                    &&!(hotelcom.contains("卫生不好"))){
                goodCom.add(hotelComs.get(i));
            }
        }
        baseResp.setData(goodCom);
        return baseResp;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResp subCom(String com, Integer oid) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String systime = df.format(System.currentTimeMillis());

        try {
            Date systimes = df.parse(systime);
            int i = hotelDao.subCom(com, oid, systimes);
            if (i != -1){
                baseResp.setMsg("评论成功!!!!!");
            }else {
                baseResp.setMsg("评论失败!!!!!!");
            }
        } catch (ParseException e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException(e);
        }
        return baseResp;
    }
//最后一笔订单时间
    @Override
    public BaseResp endOrdestime(Integer uid) {
        HotelCheckin hotelCheckin = hotelDao.endOrdestime(uid);
        System.out.println("hotelCheckin = " + hotelCheckin);
        baseResp.setData(hotelCheckin);
        return baseResp;
    }

    public BaseResp qianggouRoom(List<User> user, Integer hid){
        Hotel hotel = hotelDao.orderRoom(hid);
        if(hotel.getHnumroom()==1){
            orderRoomImpl.qgendroom(user,hid);
        }
        return null;
    }
}
